package br.inf.solutions.apresentacoes.ssp.control;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.vexillum.model.IActivatedEntity;
import br.com.vexillum.util.Return;
import br.inf.solutions.apresentacoes.ssp.control.basics.BasicFormsController;
import br.inf.solutions.apresentacoes.ssp.model.ParametrizationRemoval;
import br.inf.solutions.apresentacoes.ssp.model.basics.SpeciesRemoval;

@Service
@Scope("prototype")
public class ParametrizationRemovalController extends BasicFormsController<ParametrizationRemoval>{

	public ParametrizationRemovalController() {
		super(ParametrizationRemoval.class);
	}
	
	public Return getPresentParametrization(){
		SpeciesRemoval species = (SpeciesRemoval) (data.get("species") == null ? entity.getSpecies() : data.get("species")); 
		String sql = "FROM ParametrizationRemoval WHERE species=" + species.getId() + " AND active=true ORDER BY species.name";
		data.put("sql", sql);
		return super.searchByHQL();
	}
	
	public Return deactivatePresentParametrization(){
		ParametrizationRemoval param = (ParametrizationRemoval) data.get("presentParametrization");
		param.setActive(false);
		entity = param;
		return super.update();
	}
	
	@Override
	public Return listAll() {
		String sql = "FROM " + classEntity.getSimpleName() + " WHERE active = true ORDER BY species.name";
		if(data.get("entity") == null || (((IActivatedEntity) data.get("entity")).getActive() == null) || !((IActivatedEntity) data.get("entity")).getActive()){
			sql = sql.replace(" WHERE active = true", "");
		}
		data.put("sql", sql);
		return super.searchByHQL();
	}
	
	public Return listAllActivated() {
		String sql = "FROM " + classEntity.getSimpleName() + " WHERE active = true ORDER BY species.name";
		data.put("sql", sql);
		return super.searchByHQL();
	}
}
